<?php if (!defined('TL_ROOT')) die('You can not access this file directly!');

/**
 * TYPOlight webCMS
 *
 * The TYPOlight webCMS is an accessible web content management system that 
 * specializes in accessibility and generates W3C-compliant HTML code. It 
 * provides a wide range of functionality to develop professional websites 
 * including a built-in search engine, form generator, file and user manager, 
 * CSS engine, multi-language support and many more. For more information and 
 * additional TYPOlight applications like the TYPOlight MVC Framework please 
 * visit the project website http://www.typolight.org.
 * 
 * The Catalog extension allows the creation of multiple catalogs of custom items,
 * each with its own unique set of selectable field types, with field extendability.
 * The Front-End modules allow you to build powerful listing and filtering of the 
 * data in each catalog.
 * 
 * PHP version 5
 * @copyright	Martin Komara 2009
 * @author		Martin Komara, 
 * @package		CustomMenu
 * @license		LGPL 
 * @filesource
 */


/**
 * Class ModuleCustomMenu
 *
 * @copyright	Martin Komara 2009
 * @author		Martin Komara, 
 * @package		CustomMenu
 *
 */
class ModuleCustomMenu extends Module
{
	/**
	 * Template
	 * @var string
	 */
	protected $strTemplate = 'mod_custommenu';

	/**
	 * Display a wildcard in the back end
	 * @return string
	 */
	public function generate()
	{
		if (TL_MODE == 'BE')
		{
			$objTemplate = new BackendTemplate('be_wildcard');
			$objTemplate->wildcard = '### CUSTOM MENU ###';

			$objTemplate->title = $this->headline;
			$objTemplate->id = $this->id;
			$objTemplate->link = $this->name;
			$objTemplate->href = 'typolight/main.php?do=modules&amp;act=edit&amp;id=' . $this->id;

			return $objTemplate->parse();
		}

		return parent::generate();
	}
	
	protected function compile()
	{
		$items = $this->Database->execute("SELECT * FROM tl_custommenu ORDER BY sorting");
        $tree = new CustomMenuTree();
        $tree->loadTree($items->fetchAllAssoc());
        
        if ($this->custommenu_defineRoots)
        {
            $tree = $tree->fromRoots(deserialize($this->custommenu_roots));
        }
        
        $tree->generate();
        
        if ($this->custommenu_useChildren)
        {
            $tree = $tree->prune(1);
        }
        
        if ($this->levelOffset > 0 || $this->showLevel > 0)
        {
            $traces = array();
            if (!$this->hardLimit)
            {
                $traceFinder = new CmTraceFinder();
                $tree->accept($traceFinder);
                $traces = $traceFinder->traces;
            }
            $tree = $tree->prune($this->levelOffset, $this->showLevel-1, $traces);
        }
       
        $tp = new CmTreePrinter();
        $tree->accept($tp);
	}
}

?>
